<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header content="增加月卡" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="form" label-width="100px" :model="form" :rules="rules">
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-model="form.personName" placeholder="请输入车主姓名" />
            </el-form-item>
            <el-form-item prop="phoneNumber" label="联系方式">
              <el-input v-model="form.phoneNumber" placeholder="请输入联系方式" />
            </el-form-item>
            <el-form-item prop="carNumber" label="车辆号码">
              <el-input v-model="form.carNumber" placeholder="请输入车牌号码" />
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input v-model="form.carBrand" placeholder="请输入车辆品牌" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form ref="feeForm" label-width="100px" :model="feeForm" :rules="rules">
            <el-form-item label="有效日期" prop="payTime">
              <template>
                <el-date-picker
                  v-model="feeForm.payTime"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  value-format="yyyy-MM-dd"
                />
              </template>
            </el-form-item>
            <!-- 支付金额 -->
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="feeForm.paymentAmount" placeholder="请输入金额" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="feeForm.paymentMethod" placeholder="请选择" @change="handlerChange">
                <el-option
                  v-for="item in paymentMethods"
                  :key="item.industryCode"
                  :value="item.industryCode"
                  :label="item.industryName"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="resetBtn">重置</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { addCardAPI, editCardAPI, getCardDetailAPI } from '@/api/car'
export default {
  data() {
    return {
      form: {
        personName: '', // 车主姓名
        phoneNumber: '', // 车主电话
        carNumber: '', // 车牌号
        carBrand: '', // 车辆品牌
        cardStartDate: '', // 卡片开始时间
        cardEndDate: ''
      },
      feeForm: {
        payTime: '', // 支付时间
        paymentAmount: '', // 缴费金额
        paymentMethod: '' // 支付方式
      },
      paymentMethods: [
        { industryCode: 'Alipay', industryName: '支付宝' },
        { industryCode: 'WeChat', industryName: '微信' },
        { industryCode: 'Cash', industryName: '线下' }
      ],
      value: '', // 支付方式
      rules: {
        // 车主姓名
        personName: [
          { required: true, message: '请输入车主姓名', trigger: 'blur' }
        ],
        // 车主电话
        phoneNumber: [
          { required: true, message: '请输入车主电话', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入正确的手机号', trigger: 'blur' },
          { pattern: /^[1][3,4,5,6,7,8,9][0-9]{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        // 车牌号
        carNumber: [
          { required: true, message: '请输入车牌号', trigger: 'blur' }
          // 添加车牌号正则简单规则
        //   { parttern: /^[\u4e00-\u9fa5][A-Z][A-Z0-9]{5}$/, message: '请输入正确的车牌号', trigger: 'blur' }
        ],
        // 车辆品牌
        carBrand: [
          { required: true, message: '请输入车辆品牌', trigger: 'blur' }
        ],
        // 缴费金额
        paymentAmount: [
          { required: true, message: '请输入缴费金额', trigger: 'blur' }
        ],
        // 支付方式
        paymentMethod: [
          { required: true, message: '请选择支付方式', trigger: 'blur' }
        ],
        payTime: [
          { required: true, message: '请选择有效日期', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 判断是否有id
    console.log(this.$route.params.id)
    const id = this.$route.params.id
    if (id) {
      // 编辑状态
      this.getCardDetailAPI(id)
    }
  },
  methods: {
    // 编辑状态
    async  getCardDetailAPI(id) {
      const res = await getCardDetailAPI(id)
      // console.log(res.data.paymentMethod)
      this.form = res.data
      console.log(this.form)

      this.feeForm.payTime = [res.data.cardStartDate, res.data.cardEndDate]
      this.feeForm.paymentMethod = res.data.paymentMethod
      this.feeForm.paymentAmount = res.data.paymentAmount
    },

    // 重置
    resetBtn() {
      this.$refs.form.resetFields()
      this.feeForm.payTime = []
      this.$refs.feeForm.resetFields()
    },
    // 支付方式选择
    handlerChange(value) {
      this.paymentMethod = value
    },
    // 确定
    async confirm() {
      if (this.$route.params.id) {
        // 编辑

        const obj = { ...this.form, cardStartDate: this.feeForm.payTime[0], cardEndDate: this.feeForm.payTime[1], paymentAmount: this.feeForm.paymentAmount, paymentMethod: this.paymentMethod }
        console.log({ obj })
        delete obj.cardStatus
        await editCardAPI(obj)
        // 提醒用户修改成功
        this.$message({ type: 'success', message: '修改成功' })
      } else {
        // 新增添加
        // 发送请求
        await addCardAPI({ ...this.form, cardStartDate: this.feeForm.payTime[0], cardEndDate: this.feeForm.payTime[1], paymentAmount: this.feeForm.paymentAmount, paymentMethod: this.paymentMethod })
        // 提示用户添加成功
        this.$message({ type: 'success', message: '添加成功' })
        // 重置表单
        this.resetBtn()
      }
    }
  }

}
</script>

<style scoped lang="scss">
.add-card {
    background-color: #f4f6f8;
    height: 100vh;

    .add-header {
        display: flex;
        align-items: center;
        padding: 0 20px;
        height: 64px;
        background-color: #fff;

        .left {
            span {
                margin-right: 4px;
            }

            .arrow {
                cursor: pointer;
            }
        }

        .right {
            text-align: right;
        }
    }

    .add-main {
        background: #f4f6f8;
        padding: 20px 130px;

        .form-container {
            background-color: #fff;

            .title {
                height: 60px;
                line-height: 60px;
                padding-left: 20px;
            }

            .form {
                margin-bottom: 20px;
                padding: 20px 65px 24px;

                .el-form {
                    display: flex;
                    flex-wrap: wrap;

                    .el-form-item {
                        width: 50%;
                    }
                }
            }
        }

        .preview {
            img {
                width: 100px;
            }
        }
    }

    .add-footer {
        position: fixed;
        bottom: 0;
        width: 100%;
        padding: 24px 50px;
        color: #000000d9;
        font-size: 14px;
        background: #fff;
        text-align: center;
    }
}
</style>
